﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Data;

using System.Web;
using System.Web.UI.WebControls;

using DotNetNuke.Modules.UserDefinedTable.Security;
using DotNetNuke.Security;




namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DataTypeExpression : DataType
  {
    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      DotNetNuke.Modules.UserDefinedTable.TokenReplace replace2 = new DotNetNuke.Modules.UserDefinedTable.TokenReplace();
      replace2.ModuleId = ModuleId;
      DotNetNuke.Modules.UserDefinedTable.TokenReplace replace = replace2;

      foreach (DataRow current in ds.Tables[DataSetTableName.Fields].Rows)
      {

        if ((string)current[FieldsTableColumn.Type] == this.Name)
        {
          try
          {
            string str2 = Utilities.CStrN((current[FieldsTableColumn.InputSettings]), "");
            if ( !string.IsNullOrEmpty(str2) )
            {
              ds.Tables[DataSetTableName.Data].Columns.Remove((string)current[FieldsTableColumn.Title]);
              DataColumn column = new DataColumn((string)current[FieldsTableColumn.Title], Type.GetType("System." + str2));
              ds.Tables[DataSetTableName.Data].Columns.Add(column);
            }
            string str = replace.ReplaceEnvironmentTokens(Utilities.CStrN((current[FieldsTableColumn.Default]), ""));
            ds.Tables[DataSetTableName.Data].Columns[(string)current[FieldsTableColumn.Title]].Expression = str;
            continue;
          }
          catch (SyntaxErrorException e)
          {
            SyntaxErrorException exception = e;
            ds.Tables[DataSetTableName.Data].Columns.Remove((string)current[FieldsTableColumn.Title]);
            DataColumn column2 = new DataColumn((string)current[FieldsTableColumn.Title], typeof(string));
            ds.Tables[DataSetTableName.Data].Columns.Add(column2);
            ds.Tables[DataSetTableName.Data].Columns[(string)current[FieldsTableColumn.Title]].Expression = WarningMessage(exception.Message, ModuleId);
            continue;
          }
          catch (EvaluateException exception4)
          {
            EvaluateException exception2 = exception4;
            ds.Tables[DataSetTableName.Data].Columns.Remove((string)current[FieldsTableColumn.Title]);
            DataColumn column3 = new DataColumn((string)current[FieldsTableColumn.Title], typeof(string));
            ds.Tables[DataSetTableName.Data].Columns.Add(column3);
            ds.Tables[DataSetTableName.Data].Columns[(string)current[FieldsTableColumn.Title]].Expression = WarningMessage(exception2.Message, ModuleId);
            continue;
          }
          catch (ArgumentNullException exception5)
          {
            ArgumentNullException exception3 = exception5;
            DataColumn column4 = new DataColumn((string)current[FieldsTableColumn.Title], typeof(string));
            ds.Tables[DataSetTableName.Data].Columns.Add(column4);
            ds.Tables[DataSetTableName.Data].Columns[(string)current[FieldsTableColumn.Title]].Expression = WarningMessage(exception3.Message, ModuleId);
            continue;
          }
        }
      }
    }


    public override void SetStylesAndFormats(BoundField Column, string Format)
    {
      base.SetStylesAndFormats(Column, Format);
      if ( !string.IsNullOrEmpty(Format) )
      {
        Column.DataFormatString = string.Format("{{0:{0}}}", Format);
      }
    }

    private static string WarningMessage(string Message, int ModuleId)
    {
      Message = new PortalSecurity().InputFilter(Message.Replace("'", "''"), PortalSecurity.FilterFlag.NoMarkup);
      int tabId = -1;
      if (HttpContext.Current != null && System.Web.HttpContext.Current.Request.QueryString["tabid"] != null)
        tabId = int.Parse(System.Web.HttpContext.Current.Request.QueryString["tabid"]);
      
      if ((tabId > -1) && new ModuleSecurity(ModuleId, tabId).IsAllowedToAdministrateModule())
        return string.Format("'<span style=\"COLOR: red\">{0}</span>'", Message);
      
      return string.Format("'<span style=\"COLOR: red\">{0}</span>'", "Expression failed");
    }




    public override string InputSettingDefault
    {
      get
      {
        return "String";
      }
    }




    public override bool InputSettingsIsValueList
    {
      get
      {
        return false;
      }
    }




    public override IDictionary InputSettingsList
    {
      get
      {
        return DataType.ListOfCommonDataTypes;
      }
    }





    public override string Name
    {
      get
      {
        return "Expression";
      }
    }




    public override bool SupportsDefaultValue
    {
      get
      {
        return true;
      }
    }




    public override bool SupportsHideOnEdit
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsInputSettings
    {
      get
      {
        return true;
      }
    }




    public override bool SupportsOutputSettings
    { get { return true; } }




    public override bool SupportsSearch
    { get { return true; } }


  }

}

